1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4
5 public
class Singleton<T> : MonoBehaviour where T : Component {
6
7     
public bool destroyOnLoad;
8     
protected static bool _destroyOnLoad;
9
10     
private static T instance;
11
12     
public static T Instance {
13         
get {
14                 T foundObject = FindObjectOfType<T>();
15
16                 
if (instance == null) {
17                     instance = foundObject;
18                 }
else if (instance != foundObject) {
19                     Destroy(foundObject);
20                 }
21
22                 
if (!_destroyOnLoad) DontDestroyOnLoad(foundObject);
23                 
return instance;
24             }
25     }
26 }


Gõ tìm kiếm nhanh...